package com.prs.crm.action.ca.work;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.service.ca.work.ArticleTypesService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:33:29 物品类型
 */
public class ArticleTypesAction extends BaseAction {

	private static final long serialVersionUID = 2253002063661394067L;
	private ArticleTypes articleTypes;
	private ArticleTypesService articleTypesService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	public ArticleTypes getArticleTypes() {
		return articleTypes;
	}

	public void setArticleTypes(ArticleTypes articleTypes) {
		this.articleTypes = articleTypes;
	}

	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}

	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	
	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	@Action("listArticleTypes")
	public String listArticleType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ArticleTypes.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getArticleTypesService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editArticleTypes")
	public String editArticleType(){
		return SUCCESS;
	}	
	@Clew(results = { "success", "none" })
	@Action(value = "saveArticleTypes", results = {
			@Result(name = "success", type = "redirect", location = "listArticleTypes"),
			@Result(name = "error", type = "chain", location = "editlistArticleTypes"),
		 })
	public String saveArticleType() throws SQLException {
		if("".equals(articleTypes.getCa_at_typeName())){
			this.addActionError("类别名称不能为空！");
			return ERROR;
		}
		this.getArticleTypesService().save(articleTypes);
		this.saveEimType(articleTypes);
		this.setPrompt("保存成功！");
		return  SUCCESS;
	}
	
	@Action(value = "removeArticleTypes", results = {
			@Result(name = "success", type = "redirect", location = "listArticleTypes"),
			@Result(name = "error", type = "chain", location = "listArticleTypes") })
	public String removeArticleType(){
		if (ids==null) {
			this.addActionError("请选择操作行！");
			return ERROR;
		}
		for (Integer id : ids) {
			ArticleTypes at=this.getArticleTypesService().get(id);
			if (at.getArticleInfo().size()!=0) {
				this.addActionError("该数据有关联，不能删除！");
				return ERROR;
			}
		}
		this.getArticleTypesService().batchRemove(ids);
		this.setPrompt("删除成功！");
		return SUCCESS;
	}
	
	public void saveEimType(ArticleTypes articleTypes) throws SQLException{
		Properties pro=new Properties();
		String path = Thread.currentThread().getContextClassLoader().getResource("connect.properties").getPath();
		try {
			pro.load(new FileInputStream(path));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String ClassName=pro.getProperty("ClassName");
		String userName=pro.getProperty("userName");
		String userPass=pro.getProperty("userPass");
		String url1=pro.getProperty("url1");
		try {
			Class.forName(ClassName);
			con = DriverManager.getConnection(url1, userName, userPass);
			stmt = this.con.createStatement();
			rs=stmt.executeQuery("select * from app_articletype where typeName='"+articleTypes.getCa_at_typeName()+"'");
			if (!rs.next()) {
				String sql="insert into app_articleType(typeName,remark) values('"+articleTypes.getCa_at_typeName()+"','"+articleTypes.getCa_at_remark()+"')";
				stmt.executeUpdate(sql);
			}
			rs.close();
			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
}
